<template name="sendcar">
	<!-- 移位的物资列表 -->
	<view class="mypage">
		<view class="cu-bar search bg-white">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input type="text" placeholder="请输入物资名称" v-model="keyword" />
				<input type="text" placeholder="请输入物资规格" v-model="keyword2" />
			</view>
			<view class="action">
				<button class="cu-btn bg-blue shadow-blur round" @tap="handelSearch">搜索</button>
			</view>
		</view>

		<view class="card-list-wrapper">
			<view class="card-list-row radius shadow-warp bg-white margin-top" v-for="(item,index) in planList"
				:key="index" @click="openDetail(index)">
				<view class="flex-just-between bottom-border top-wrap">
					<text class="text-blue text-bold">物资单号：{{ item[config.materId] || '' }}</text>
					<view class="cu-capsule radius">
						<view class="cu-tag bg-blue sm">
							<text class="cuIcon-medal"></text>
						</view>
						<view class="cu-tag line-brown sm">{{ index+1}}</view>
					</view>
				</view>

				<view class="card-list-row-1 margin-top">
					<text class="cuIcon-title text-red"></text>
					<text>物资：{{ item[config.materName] || '' }}</text>
				</view>

				<view class="card-list-row-1">
					<text class="cuIcon-title text-orange"></text>
					<text>规格：{{ item.MATSPEC || '' }}</text>
				</view>

				<view class="card-list-row-1">
					<text class="cuIcon-title text-orange"></text>
					<text>设备：{{ item.EQUIPNAME || '' }}</text>
				</view>

				<view class="card-list-row-1">
					<text class="cuIcon-title text-orange"></text>
					<text>状态：{{ getStatus(item.BILLSTATUS)   }}</text>
				</view>

				<view class="position-a checkFlag" v-if="item.ischeck">
					<text class="cuIcon-squarecheckfill text-blue"></text>
				</view>
			</view>
		</view>

		<!-- <view class="next-btn-wrap position-f" @click="nextTap">
			<button class="big-btn bg-green shadow-blur">
				<text class="refresharrow text-bold"></text>
			</button>
		</view> -->

		<view class="fixed-btn">
			<view class="btn btn-left text-blue" @click="SubmitEvent(1)">
				维修完成
			</view>
			<view class="btn btn-left text-red" @click="SubmitEvent(2)">
				申请报废
			</view>
		</view>

		<modal title="高级查询" placeholder :isShow="isShowModal" @cancelModal="cancelModal" @okModal="okModal"></modal>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from "vuex";
	import api from "@/common/api.js";
	import utils from "@/common/util.js";
	import modal from "@/components/modal/modal_mater.vue";
	let query = {
		category: "",
		page: 1,
		rows: "10",
		sidx: "CREATIONTIME",
		sord: "desc",
		queryJson: "",

		TYPE: "",
		userId: "",
		account: ""
	};
	let _self,
		menuType,
		page = 1,
		billID,
		total = 1,
		goPath = "",
		pageTitle = "待维修(",
		PK_STORE = '',
		queryJson = {};

	export default {
		computed: mapState(["userInfo", "baseUrl", "checkData"]),
		data() {
			return {
				planList: [],
				keyword: "",
				keyword2: "",
				category: "",
				isShowModal: false,
				config: {
					materId: "",
					materName: ""
				},
				statusName: [{
					"id": "0",
					"name": "维修中"
				}, {
					"id": "1",
					"name": "报废"
				}, {
					"id": "2",
					"name": "维修完成"
				}]
			};
		},

		onLoad(p) {
			// console.log(p)
			_self = this;
			this.setCheckData([]);
			menuType = 'ST_STOCKINFO';
			PK_STORE = p.PK_STORE
			queryJson = {}
			getApp().globalData.queryJsonLS = null 
			queryJson.PK_STORE = p.PK_STORE
			queryJson.BILLSTATUS = '0'
			this.initData();
		},
		components: {
			modal
		},
		onNavigationBarButtonTap(e) {
			if(e.index==0){
				this.listenOnce()
				uni.navigateTo({
					url: '/pages/public/query?type=menu1017' 
				})
			}else{
				_self.openScanMater();
			}
		},
		onShow() {
			// 刷新列表
			uni.$once("refreshList", () => {
				page = 1;
				total = 1;
				this.getList();
			});
		},
		
		//下拉刷新
		onPullDownRefresh: function() {
			// 重置分页及数据
			this.handelSearch()
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
			uni.showToast({
				title: "数据已刷新",
				icon: "none"
			});
		},
		// 加载更多
		onReachBottom: function() {
			this.getList();
		},
		methods: {
			...mapMutations(["setCheckData"]),
			initData() {
				query.category = "ST_STOCKINFO";
				query.TYPE = ""
				this.config.materId = "PK_MATERIAL";
				this.config.materName = "MATERIALNAME";
				// 手工单出库 + 新增出库申请单 使用库存物资
				query.userId = _self.userInfo.UserId;
				query.account = _self.userInfo.Account;
				queryJson.PK_ORG = _self.userInfo.ORGANIZEID;
				this.category = query.category;
				page = 1;
				total = 1;
				this.getList();
			},
			
			// 扫描设备
			openScanMater() {
				this.$utils.openScanUtils(0, 0, (res) => {
					if (res.includes('TYPE:8')) {
						let arr = res.split('|');
						let AddItem = {
							PK_EQUIP: '',
							EQUIPNAME: '',
							IMPUNUM: ''
						}
						queryJson.EQUIPNAME = arr[1]
						page = 1;
						total = 1;
						_self.planList = [];
						_self.getList();
					} else {
						this.$utils.tips('非设备二维码,无法使用')
					}
				})
			},

			getStatus(t) {
				let s = this.statusName.filter(i => {
					return i.id == t
				})
				let n = s.length ? s[0].name : '维修中'
				return n;
			},

			// STATIONNAME 遍历仓库

			mapSattionName(data) {
				if (!data) return [];
				let arr = JSON.parse(data);
				return arr;
			},

			openDetail(e) {
				_self.planList[e].ischeck = !_self.planList[e].ischeck;
				_self.planList[e].myIndex = e;
			},
			
			// 初始监听
			listenOnce() {
				uni.$off('reload')
				uni.$once("reload", function(data) {
					queryJson= { ...queryJson , ...data }
					getApp().globalData.queryJsonLS = queryJson
					_self.handelSearch()
				});
			},
			
			handelSearch() {
				// queryJson.BILLNO = _self.keyword;
				//   let a = {
				//     MATSPEC: _self.keyword2,
				//     MATERIALNAME: _self.keyword,
				//     VNAME: _self.keyword,
				//     BILLNO: "",
				// PK_ORG: _self.userInfo.ORGANIZEID
				//   };
				//  queryJson = { ...a };

				queryJson.MATSPEC = _self.keyword2
				queryJson.MATERIALNAME = _self.keyword
				queryJson.VNAME = _self.keyword
				queryJson.PK_STORE = PK_STORE || ''
				queryJson.PK_ORG = _self.userInfo.ORGANIZEID
				queryJson.BILLSTATUS = '0'

				page = 1;
				total = 1;
				_self.planList = [];
				this.getList();
			},
			nextTap() {
				let t = [];
				_self.planList.forEach(item => {
					if (item.ischeck) {
						t.push(item);
					}
				});
				let newCheckData = []; // 如果新选择的数据不存在 已选择中 进行添加到 chekData
				let oldCheckDataPK = '';
				_self.checkData.forEach(item => {
					oldCheckDataPK += item.PK_MATERIAL + '-';
				})

				if (_self.checkData.length == 0) {
					newCheckData = [...t]
				} else {
					t.forEach(item => {
						if (oldCheckDataPK.indexOf(item.PK_MATERIAL) < 0) {
							newCheckData.push(item)
						}
					})
				}
				let newCheckData2 = [...newCheckData, ...this.checkData];
				this.setCheckData(newCheckData2);
				if (newCheckData2.length < 1) return _self.$utils.tips("请至少选择一个物资！");
				uni.navigateBack({})
				// uni.navigateTo({
				//   url: goPath + encodeURIComponent(JSON.stringify(t))
				// });
			},

			getList() {
				if (page > total) {
					return utils.tips("没有数据了");
				}
				query.page = page;
				query.queryJson = JSON.stringify(queryJson);
				console.log(page , query);
				let URL = '/API/APPCommon/GetEquipmentRepair'
				this.axios({
					url: URL,
					data: query,
					isLoading: 1
				}).then(res => {
					let {
						Result
					} = res;
					let array = Result.rows.map(item => {
						item.ischeck = false;
						return item;
					});
					if (page == 1) {
						_self.planList = array;
						if (_self.planList.length == 0) {
							utils.tips("暂无数据！");
						}
						_self.setPageTitle(pageTitle + Result.records + ")");
					} else {
						_self.planList = _self.planList.concat(array);
					}
					queryJson = {};
					total = Result.total;
					page++;
				})
			},
			setPageTitle(d) {
				uni.setNavigationBarTitle({
					title: d
				});
			},

			// 控制高级查询条件
			cancelModal() {
				this.isShowModal = false;
			},
			okModal(val) {
				this.cancelModal();
				queryJson = {
					...val
				};
				page = 1;
				total = 1;
				_self.planList = [];
				this.getList();
			},

			SubmitEvent(t) {
				let Arr = _self.planList.filter(i => i.ischeck)
				if (!Arr.length) {
					return _self.$utils.tips("请至少选择一个物资！")
				}
				let title = t == '1' ? '维修完成' : '申请报废'
				let arrStr = Arr.map(i => {
					return i.BILLNO
				}).join(',')
				
				if(t == 2){
					uni.navigateTo({
						url: '/pages/tab02/onsite?t=4&item=' + encodeURIComponent(JSON.stringify(Arr[0]))
					});
					return ;
				}
				
				uni.showModal({
					title: '提示',
					content: '您确定要执行' + title + '操作吗？',
					success(res) {
						if (res.confirm) {
							_self.submitData(t, arrStr, Arr[0])
						}
					}
				})
			},
			submitData(t, data , item ) {
				

				let savedata = {
					"DETAILS": data,
					"keyValue": data
				}
				let URL = t == 1 ? '/API/APPCommon/SaveEquipmentRepair' : '/API/APPCommon/SaveEquipmentScrap'
				this.axios({
					url: URL,
					data: savedata,
					isLoading: 1
				}).then(res => {
					_self.$utils.tips('提交成功')
					setTimeout(a => {
						uni.navigateBack({})
					}, 1500)
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	page {
		padding-bottom: 100rpx;
		box-sizing: border-box;
	}

	.card-list-wrapper {
		width: 710upx;
		margin: 0 auto;
	}

	.card-list-row {
		padding: 20upx;
	}

	.card-list-row-1 {
		padding: 10upx 0;
	}



	.top-border {
		border-top: 1upx solid rgba(0, 0, 0, 0.1);
	}

	.top-wrap {
		padding-bottom: 20upx;
	}

	.bottom-border {
		border-bottom: 1upx solid rgba(0, 0, 0, 0.1);
	}

	.btn-wrap {
		padding-top: 20upx;
	}

	.checkFlag {
		top: 50%;
		right: 20upx;
		font-size: 40upx;
	}

	.next-btn-wrap {
		position: fixed;
		right: 50upx;
		bottom: 100upx;
	}

	.big-btn {
		width: 100upx;
		height: 100upx;
		border-radius: 50upx;
		font-size: 40upx;
	}

	.fixed-btn {
		position: fixed;
		bottom: 0%;
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #fff;
		display: flex;
		justify-content: space-between;

		.btn {
			text-align: center;
			width: 50%;

		}

		.btn-left {
			border-right: 1rpx solid #ddd;
		}
	}
</style>